
<template>
    <div class="container">
        <section v-if="show" style="width: 450px; height: 100%; float: right;">
            <client-only>
                <el-amap
                    id="amap"
                    class="amap-demo"
                    :center="center"
                    :events="events"
                    :zooms="zooms"
                    :amap-manager="amapManager"
                >
                    <el-amap-marker
                        v-for="(marker, index) in markers"
                        :key="index"
                        :ref="`marker${index}`"
                        :position="marker.position"
                        :vid="index"
                        :events="marker.events"
                        :content="marker.content"
                        :z-index="marker.zIndex"
                        animation="AMAP_ANIMATION_DROP"
                    />
                </el-amap>
            </client-only>
        </section>
        <img v-else :src="require('@/static/loading.gif')" alt="" style="width: 450px; height: 100%; float: right;">
        <div v-show="tip.show" class="tip">
            <div
                role="tooltip"
                aria-hidden="false"
                class="el-popover"
                :style="tip.styleObject"
            >
                {{ tip.content }}
            </div>
        </div>
    </div>
</template>

<script>
    // public static CameraUpdate zoomIn();
    import VueAMap from "vue-amap";
    import * as _ from "lodash";
    const amapManager = new VueAMap.AMapManager();
    export default {
        data () {
            const self = this;
            return {
                key: "34723cdff8d29820decbe8b8284ecfbb",
                map_data: {

                },
                show: false,
                center: [],
                zooms: [3, 18],
                amapManager,
                events: {
                    init (map) {
                        const markers = _.cloneDeep(self.markers);
                        markers.forEach((item, index) => {
                            // eslint-disable-next-line no-undef
                            AMapUI.loadUI(["overlay/SimpleMarker"], function (SimpleMarker) {
                                item = new SimpleMarker({
                                    iconLabel: {
                                        innerHTML: index,
                                        style: {
                                            color: "#fff"
                                        }
                                    },
                                    iconStyle: "#1995f5",
                                    map,
                                    position: item.position
                                });
                            });
                        });
                    },
                    zoomstart () {
                        self.tip.timeID = setInterval(() => {
                            self.handleScroll();
                        }, 50);
                    },
                    zoomend () {
                        clearInterval(self.tip.timeID);
                    },
                    mapmove () {
                        self.handleScroll();
                    }
                },
                lng: 0,
                lat: 0,
                markers: [],
                city_data: {},
                top: "100px",
                left: "100px",
                tip: {
                    styleObject: {
                        "min-width": "10px",
                        "z-index": 2036,
                        display: "block",
                        padding: "5px",
                        position: "fixed",
                        top: "100px",
                        left: "100px",
                        "margin-left": "11px",
                        transform: "translateX(-50%)"
                    },
                    content: "提示",
                    show: false,
                    zIndex: 100,
                    activeIndex: 0,
                    timeID: ""
                }
            };
        },
        created () {
            // const cityName = this.$route.query.cityName;
            // if (!cityName) {
            this.get_ip_position();
            // }
        },
        beforeDestroy () {
            clearInterval(this.tip.timeID);
        },
        methods: {
            async get_ip_position () {
                const res = await this.$axios({
                    method: "GET",
                    url: "https://restapi.amap.com/v3/ip?parameters",
                    params: {
                        key: this.key
                    }
                });
                this.map_data = res.data;
                // 提示城市 调试时未开启
                // this.$confirm(`定位当前城市: ${this.map_data.city}`, "提示", {
                //     confirmButtonText: "确定",
                //     showCancelButton: false,
                //     type: "success"
                // });
                // console.log(res);
                this.$router.push({
                    query: {
                        cityName: this.map_data.city
                    }
                });
                const positionList = this.map_data.rectangle.split(";");
                this.center = [(Number(positionList[0].split(",")[0]) + Number(positionList[1].split(",")[0])) / 2, (Number(positionList[0].split(",")[1]) + Number(positionList[1].split(",")[1])) / 2];
                this.get_city_data();
                this.map_show();
            },
            map_show () {
                // 确认加载地图api完成
                if (typeof (AMapUI) === "undefined") {
                    setTimeout(() => {
                        this.map_show();
                    }, 100);
                } else {
                    this.show = true;

                    // 缩放
                    setTimeout(() => {
                        const o = amapManager.getMap();
                        o.setZoomAndCenter(12);
                    }, 1000);
                }
            },
            async get_city_data () {
                const res = await this.$axios({
                    method: "GET",
                    url: "/cities",
                    params: {
                        name: this.map_data.city
                    }
                });
                this.city_data = res.data.data[0];
                const hotelRes = await this.$axios({
                    method: "GET",
                    url: "http://157.122.54.189:9095/hotels",
                    params: {
                        city: this.city_data.id
                    }
                });
                console.log(hotelRes);
                const hotelList = hotelRes.data.data.map((Element, index) => {
                    return {
                        position: [Element.location.longitude, Element.location.latitude],
                        events: {
                            mouseover: () => {
                                // 用于外部方法调用
                                this.tip.activeIndex = index;
                                this.tip.zIndex++;
                                this.markers[index].zIndex = this.tip.zIndex;
                                const markDom = document.getElementById(index);
                                this.tip.styleObject.top = `${markDom.getBoundingClientRect().top - 40}px`;
                                this.tip.styleObject.left = `${markDom.getBoundingClientRect().left}px`;
                                this.tip.content = Element.name;
                                this.tip.show = true;
                            },
                            mouseout: () => {
                                this.tip.show = false;
                            },
                            click: () => {
                                this.tip.zIndex++;
                                this.markers[index].zIndex = this.tip.zIndex;
                                const markDom = document.getElementById(index);
                                this.center = [Element.location.longitude, Element.location.latitude];
                                this.tip.show = false;
                                setTimeout(() => {
                                    this.tip.styleObject.top = `${markDom.getBoundingClientRect().top - 40}px`;
                                    this.tip.styleObject.left = `${markDom.getBoundingClientRect().left}px`;
                                    this.tip.content = Element.name;
                                    this.tip.show = true;
                                }, 500);
                            }
                        },
                        visible: true,
                        draggable: false,
                        content: `<span class="marker" id="${index}">${index + 1}</span>`,
                        title: `${Element.name}`,
                        zIndex: 100
                    };
                });
                this.markers = [...hotelList];
                console.log(this.markers);
                const positionXList = []; const positionYList = [];
                this.markers.forEach((Element) => {
                    positionXList.push(Element.position[0]);
                    positionYList.push(Element.position[1]);
                });
                const X = (Math.max(...positionXList) + Math.min(...positionXList)) / 2;
                const Y = (Math.max(...positionYList) + Math.min(...positionYList)) / 2;
                this.center = [X, Y];
            },
            handleScroll () {
                if (this.tip.show === true) {
                    const markDom = document.getElementById(this.tip.activeIndex);
                    this.tip.styleObject.top = `${markDom.getBoundingClientRect().top - 40}px`;
                    this.tip.styleObject.left = `${markDom.getBoundingClientRect().left}px`;
                }
            }
        }
    };
</script>

<style lang="less" scoped>
.container {
    width: 100%;
    height: 100%;
}
/deep/.marker {
    display: inline-block;
    width: 22px;
    height: 36px;
    background-image: url(https://webapi.amap.com/theme/v1.3/markers/b/mark_b.png);
    background-size: 22px 36px;
    text-align: center;
    line-height: 24px;
    color: #fff;
}
</style>
